package com.ztjc.day03_dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;

/**
 * Android中对话框的使用
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 对话框
     * @param view
     */
    public void dialog(View view) {
        new AlertDialog.Builder(this).setTitle("提示！").setMessage("欲练此功，必先自宫").setNegativeButton("仔细想想", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "若不自宫，定不成功", Toast.LENGTH_SHORT).show();
            }
        }).setPositiveButton("马上自宫", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "恭喜自宫，开始练功", Toast.LENGTH_SHORT).show();
            }
        }).show();
    }

    /**
     * 单选对话框
     * @param view
     */
    public void dialog2(View view) {
        final String[] items = {"男", "女", "保密"};
        new AlertDialog.Builder(this).setTitle("选择性别").setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "你的性别是：" + items[which], Toast.LENGTH_SHORT).show();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        }).show();
    }

    /**
     * 多选对话框
     * @param view
     */
    public void dialog3(View view) {
        final boolean[] checkedItems = {false, false, false, false};
        final String[] items = {"苹果", "香蕉", "梨子", "桃子"};
        new AlertDialog.Builder(this).setTitle("选择喜欢的水果").setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                checkedItems[which] = isChecked;
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String result = "";
                for (int i = 0; i < items.length; i++) {
                    if (checkedItems[i]) {
                        result += items[i] + ",";
                    }
                }
                Toast.makeText(MainActivity.this, "你喜欢：" + result, Toast.LENGTH_SHORT).show();
            }
        }).show();
    }
}
